﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using Lamex.Interface;
using Lamex.Library.CommonClass;

namespace Lamex.mMaterial.UI
{
    public partial class frmMC_MO_BOM : frmBaseChild
    {
        string strSql;
        DataTable dt;

        public frmMC_MO_BOM()
        {
            InitializeComponent();
        }

        private void frmMC_MO_BOM_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            //strSql = @"SELECT Package_Mo FROM View_OP_Follow_Owed_QTY ORDER BY Package_Mo DESC";
            //dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            //comboBoxMO_No.DataSource = dt;
            //comboBoxMO_No.DisplayMember = "Package_Mo";
            //if(Lamex.Common.Loginer.CurrentUser.Account == "Rocky He")
            //{
            //    //comboBoxMO_No.Text = "648146024209";
            //}
            //else
            //{
            //    comboBoxMO_No.Text = "";
            //}
        }

        private void comboBoxMO_No_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                refreshGridView_ByOrderNo();
            }
        }

        #region //控件关联操作

        private void radioButtonByOrderNo_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonByOrderNo.Checked == true)
            {
                groupBoxByOrderNo.Enabled = true;
                groupBoxByDate.Enabled = false;

                dateEditStart.Text = "";
                dateEditEnd.Text = "";
            }
            gridView1.Columns.Clear();
            gridView2.Columns.Clear();
        }

        private void radioButtonByDate_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonByDate.Checked == true)
            {
                groupBoxByOrderNo.Enabled = false;
                groupBoxByDate.Enabled = true;

                comboBoxMO_No.Text = "";
                textBoxMO_Desc.Text = "";
                textBoxQTY.Text = "";
                textBoxOwed_QTY.Text = "";
            }
            gridView1.Columns.Clear();
            gridView2.Columns.Clear();
        }

        private void cboxMO_BOM_TextChanged(object sender, EventArgs e)
        {
            textBoxMO_Desc.Text = "";
            textBoxQTY.Text = "";
            textBoxOwed_QTY.Text = "";
        }

        #endregion

        private void sbtnView_Click(object sender, EventArgs e)
        {
            if (comboBoxMO_No.Text != "")
            {
                refreshGridView_ByOrderNo();
            }
            else
            {
                if (dateEditEnd.Text == "")
                {
                    refreshGridView_ByDate("1=1");
                }
                else
                {
                    refreshGridView_ByDate("Product_TS<='" + dateEditEnd.Text + "'");
                }
            }
        }

        private void refreshGridView_ByOrderNo()
        {
            strSql = @"SELECT Material_Desc,sum1,OWED_QTY 
                        FROM View_OP_Follow_Owed_QTY
                        WHERE Package_Mo='" + comboBoxMO_No.Text + @"' ";

            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            textBoxMO_Desc.Text = dt.Rows[0][0].ToString();
            textBoxQTY.Text = dt.Rows[0][1].ToString();
            textBoxOwed_QTY.Text = dt.Rows[0][2].ToString();

            //因订单明细（649262000209）存在只有母体的情况，所以不过滤 Part_No!=Material_No 
            strSql = @"SELECT Part_No,Part_QTY,Order_Number,Drawing_No,Part_Desc
                        FROM Transfer_Part_Detailed
                        WHERE Package_Mo='" + comboBoxMO_No.Text + @"' ";
            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            gridControl1.DataSource = dt;
            gridView1.OptionsBehavior.Editable = false;
            gridView1.OptionsView.ColumnAutoWidth = true;

            strSql = @"SELECT Material_No,Material_Desc,SUM(Demand_QTY) AS QTY,Base_Unit
                        FROM Transfer_MO_BOM
                        WHERE Package_Mo_Main='" + comboBoxMO_No.Text + @"'
						GROUP BY Material_No,Material_Desc,Base_Unit";
            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            gridControl2.DataSource = dt;
            gridView2.OptionsBehavior.Editable = false;
            gridView2.OptionsView.ColumnAutoWidth = true;

            //648284000409
        }

        private void refreshGridView_ByDate(string strCondition)
        {
            strSql = @"SELECT Product_TS,Package_Mo,Order_QTY,Material_Desc,
						Part_No,Part_QTY,Order_Number,Drawing_No,Part_Desc
                        FROM Transfer_Part_Detailed
						WHERE Product_TS>='" + dateEditStart.Text + @"' AND " + strCondition;

            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            gridControl1.DataSource = dt;
            gridView1.OptionsBehavior.Editable = false;
            gridView1.OptionsView.ColumnAutoWidth = true;

            strSql = @"SELECT Material_No,Material_Desc,SUM(Demand_QTY) AS QTY,Base_Unit
                        FROM Transfer_MO_BOM
						WHERE Package_Mo_Main IN
 						(SELECT DISTINCT Package_Mo
						FROM Transfer_Package_List
						WHERE Product_TS>='" + dateEditStart.Text + @"' AND " + strCondition + @")                       
						GROUP BY Material_No,Material_Desc,Base_Unit";

            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            gridControl2.DataSource = dt;
            gridView2.OptionsBehavior.Editable = false;
            gridView2.OptionsView.ColumnAutoWidth = true;
        }

        //gridView仅复制单元格内容，复制选中行焦点单元格的值
        private void gridView2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control & e.KeyCode == Keys.C)
            {
                Clipboard.SetDataObject(gridView2.GetFocusedRowCellValue(gridView2.FocusedColumn));
                e.Handled = true;
            }

            //或设置属性
            //optionBehavior - CopyToClipboardWithColumnHeader = false;
            //optionSelection - MultiSelectMode = CellSelect;
        }

        private void 隐藏选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 0;
        }

        private void 显示选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 60;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmMC_BOM_Query), sender as ToolStripMenuItem);
        }
    }
}